package org.znxs.znxsojsandbox.docker;


import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.Container;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient;
import com.github.dockerjava.transport.DockerHttpClient;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.util.List;

@Slf4j
public class DockerDemo {

    private static final String IMAGE_NAME = "nginx:latest";


    public static void main(String[] args) {
        // 获取默认的docker 客户端
//        DockerClient dockerClient = DockerClientBuilder.getInstance().build();
//        PingCmd pingCmd = dockerClient.pingCmd();
        String dockerHost = "npipe:////./pipe/dockerDesktopLinuxEngine";

        DockerHttpClient dockerHttpClient = new ApacheDockerHttpClient.Builder().dockerHost(URI.create(dockerHost)).maxConnections(100).connectionTimeout(Duration.ofSeconds(30)).responseTimeout(Duration.ofSeconds(45)).build();

        DockerClient dockerClient = DockerClientBuilder.getInstance(dockerHost).withDockerHttpClient(dockerHttpClient).build();

        // 使用dockerClient进行操作
        PingCmd pingCmd = dockerClient.pingCmd();
        pingCmd.exec();
        log.info("执行成功");
        // 下载容器
//        downImage(dockerClient);
        // 创建容器
//        createContainer(dockerClient);
        // 查看容器状态
//        catContainerStatus(dockerClient);
        // 运行容器
        runContainer(dockerClient);


        // 不再需要dockerClient时关闭它
        try {
            dockerClient.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }


    private static void downImage(DockerClient dockerClient) {

        PullImageCmd pullImageCmd = dockerClient.pullImageCmd(IMAGE_NAME);
        PullImageResultCallback pullImageResultCallback = new PullImageResultCallback() {
            @Override
            public void onNext(PullResponseItem item) {
                log.info("下载镜像：" + item.getStatus());
                super.onNext(item);
            }
        };
        try {
            pullImageCmd.exec(pullImageResultCallback).awaitCompletion();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("下载完成");
    }

    private static void createContainer(DockerClient dockerClient) {
        // 创建容器

        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(IMAGE_NAME);
        CreateContainerResponse createContainerResponse = containerCmd.withCmd("echo", "Hello Docker").exec();
        log.info(String.valueOf(createContainerResponse));

    }


    private static void catContainerStatus(DockerClient dockerClient) {
        ListContainersCmd listContainersCmd = dockerClient.listContainersCmd();
        List<Container> containerList = listContainersCmd.withShowAll(true).exec();
        for (Container container : containerList) {
            log.info(String.valueOf(container));
        }
    }

    private static void runContainer(DockerClient dockerClient) {
        // 运行容器
        dockerClient.startContainerCmd("abfacd16b424ef3542890b5072a11c77931c9de0c4eee14130a353d45200cb81").exec();

    }

}
